package com.example.ximalaya.adapters;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.ximalaya.R;
import com.example.ximalaya.utils.LogUtil;
import com.squareup.picasso.Picasso;
import com.ximalaya.ting.android.opensdk.model.album.Album;

import java.util.ArrayList;
import java.util.List;

public class RecommendListAdapter extends RecyclerView.Adapter<RecommendListAdapter.InnerHolder> {
     private List<Album> mData=new ArrayList<>();
     private static final String TAG="RecommendListAdapter";
    @Override
    public InnerHolder onCreateViewHolder(@NonNull  ViewGroup parent, int viewType) {
        //这里是加载view
        //这里需要加载布局的话，只需传入当前view的context就行
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recommend, parent, false);
        //此时的getContext返回的是当前View运行在哪个Activity Context中，也就是Activity上下文对象
        LogUtil.d(TAG,itemView.getContext()+"itemView.getContext()");
        LogUtil.d(TAG,parent.getContext()+"parent.getContext()");
        return new InnerHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull  RecommendListAdapter.InnerHolder holder, int position) {
        //这里是设置数据
        //把当前的位置设置给itemView
        holder.itemView.setTag(position);
        holder.setData(mData.get(position));

        LogUtil.d("RecommendListAdapter",position+"位置");
    }

    @Override
    public int getItemCount() {
        //返回显示的个数
        if(mData!=null){
            LogUtil.d("RecommendListAdapter",mData.size()+"数目");
            return mData.size();
        }
        return 0;
    }

    public void setData(List<Album> albumList) {
        if(mData!=null){
            //记得先清理一下，再全部数据给mData，这样才是最新的
            mData.clear();
            mData.addAll(albumList);
        }
        //一般每次添加完数据都需要，更新一下UI(刷新数据)
        notifyDataSetChanged();//他会重新再执行一次适配器的方法再次更新ui

    }

    public class InnerHolder extends RecyclerView.ViewHolder {

        public InnerHolder(@NonNull  View itemView) {
            super(itemView);
        }

        public void initView(){

        }

        public void setData(Album album) {
            //找到各个控件,设置数据
            //专辑封面
            ImageView albumCoverIv = itemView.findViewById(R.id.album_cover);
            //title
            TextView albumTitleTv=itemView.findViewById(R.id.album_title_tv);
            //描述
            TextView albumDesTv = itemView.findViewById(R.id.album_description_tv);
            //播放数量
            TextView albumPlayCountTv=itemView.findViewById(R.id.album_play_count);
            //专辑内容数量
            TextView albumContentCountTv=itemView.findViewById(R.id.album_content_size);


            albumTitleTv.setText(album.getAlbumTitle());
            albumDesTv.setText(album.getAlbumIntro());
            albumPlayCountTv.setText(album.getPlayCount()+"");
            albumContentCountTv.setText(album.getIncludeTrackCount()+"");
            LogUtil.d(TAG,album.getIncludeTrackCount()+"");
            //用于加载到当前布局的图片,与当前布局的生命周期一样直接获取当前布局的context
            Picasso.with(itemView.getContext()).load(album.getCoverUrlLarge()).into(albumCoverIv);

        }
    }
}
